package com.xxx.dorm.dao.impl;

import com.xxx.dorm.dao.BuildingDao;
import com.xxx.dorm.domain.Building;
import com.xxx.dorm.domain.Dormitory;
import com.xxx.dorm.domain.Manage;
import com.xxx.dorm.utils.JDBCUtils;
import org.springframework.cglib.beans.BulkBean;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class BuildingDaoImpl implements BuildingDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 通过寝室表中的bid(即building表的id)查找宿舍楼信息
     * @param bid
     * @return
     */
    @Override
    public Building findByBid(Integer bid) {
        String sql;
        sql = "SELECT * FROM t_building WHERE id = ?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<>(Building.class),bid);
    }

    /**
     * 查找所有宿舍楼名
     * @return
     */
    @Override
    public List<Building> findName() {
        String sql;
        sql = "SELECT name FROM t_building";
        return template.query(sql,new BeanPropertyRowMapper<>(Building.class));
    }

    @Override
    public List<Building> findAll() {
        String sql;
        sql = "SELECT * FROM t_building";
        return template.query(sql,new BeanPropertyRowMapper<>(Building.class));
    }

    @Override
    public List<Building> find(String conditions, String information) {
        String sql = null;
        if(conditions.equals("name")){
            sql = "select * from t_building where name = ?";
        }
        return template.query(sql,new BeanPropertyRowMapper<>(Building .class),information+"");
    }

    @Override
    public void addBuild(Building building) {
        String sql = "insert into t_building(name,capablePeople,actualPeople) values (?,?,?)";
        template.update(sql,building.getName(),building.getCapablePeople(),building.getActualPeople());
    }

    @Override
    public void deleteBuild(Integer id) {
        String sql = null;
        sql = "delete from t_building where id = ?";
        template.update(sql,id);
    }

    @Override
    public void updateBuild(Building building) {
        String sql = "update t_building set name = ? where id = ?";
        template.update(sql,building.getName(),building.getId());
    }

    /**
     * 增加容纳人数
     * @param dormitory
     */
    @Override
    public void addCapablePeople(Dormitory dormitory) {
        String sql = null;
        sql = "SELECT capablePeople FROM t_building WHERE t_building.id = ?";
        Integer oldnum = template.queryForObject(sql,Integer.class,dormitory.getBid());
        sql = "UPDATE t_building set capablePeople = ? WHERE t_building.id = ?";
        template.update(sql,Integer.valueOf(dormitory.getCapablePeople())+oldnum,dormitory.getBid());
    }
    /**
     *减少容纳人数
     * @param dormitory
     */
    @Override
    public void reduceCapablePeople(Dormitory dormitory) {
        String sql = null;
        sql = "SELECT capablePeople FROM t_building WHERE t_building.id = ?";
        Integer oldnum = template.queryForObject(sql,Integer.class,dormitory.getBid());
        sql = "UPDATE t_building set capablePeople = ? WHERE t_building.id = ?";
        template.update(sql,oldnum-Integer.valueOf(dormitory.getCapablePeople()),dormitory.getBid());
    }

    /**
     * 通过bid删除中间件
     * @param bid
     */
    @Override
    public void deleteMiddleware(Integer bid) {
        String sql = "delete from t_manage_building where bid = ?";
        template.update(sql,bid);
    }

    /**
     * 学生增加修改宿舍实际人数
     * @param bid
     */
    @Override
    public void addActualPeople(Integer bid) {
        String sql = null;
        sql = "SELECT actualPeople FROM t_building WHERE t_building.id = ?";
        Integer oldnum = template.queryForObject(sql,Integer.class,bid);
        sql = "UPDATE t_building set actualPeople = ? WHERE t_building.id = ?";
        template.update(sql,Integer.valueOf(oldnum)+1,bid);
    }

    /**
     * 学生删除修改宿舍实际人数
     * @param bid
     */
    @Override
    public void reduceActualPeople(Integer bid) {
        String sql = null;
        sql = "SELECT actualPeople FROM t_building WHERE t_building.id = ?";
        Integer oldnum = template.queryForObject(sql,Integer.class,bid);
        sql = "UPDATE t_building set actualPeople = ? WHERE t_building.id = ?";
        template.update(sql,Integer.valueOf(oldnum)-1,bid);
    }
}
